﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.NetworkInformation;
using System.Drawing;
using BaseHelper;
using DeviceSDK.Park.Vz;

namespace DeviceSDK.Park
{
    public class ParkHelper
    {

        private static SortedList<string, string> slDeviceSdk = new SortedList<string, string>();


        //设备-获取-状态
        public static long Ping(string ip)
        {
            Ping ping = new Ping();
            try
            {

                PingReply pingReply = ping.Send(ip);
                if (pingReply.Status == IPStatus.Success)
                    return pingReply.RoundtripTime;
            }
            catch (Exception ex)
            {

            }
            finally
            {
                ping.Dispose();
            }
            return -1;
        }

        public static void Init(string pid,string tempfolder)
        {
            if (pid == "VZSDK")
            {
                VzSDK.Init(tempfolder);                  
            }
        }

        public static void Close(string ip)
        {
            try
            {
                if (!slDeviceSdk.ContainsKey(ip))
                    return;

                string pid = slDeviceSdk[ip];
                if (pid == "VZSDK")
                {
                    VzSDK.Close(ip);
                }

                if (slDeviceSdk.ContainsKey(pid))
                    slDeviceSdk.Remove(pid);
            }
            catch (Exception ex)
            {
                LogHelper.Err("RTDevice.DeviceHelper.cs", "Close(" + ip + ")", ex);
            }
        }

        public static string GetError(string pid,int err)
        {
            if (pid == "VZSDK")
                return VzSDK.GetError(err);

            return "未知SDK名称!";

        }

        public static int SetListen(DeviceInfo d)
        {
            return SetListen(d.SdkID,d.DeviceName,d.IP,d.LoginName,d.LoginPass,d.Port);
        }

        public static int SetListen(string pid,string caption, string ip, string user, string pass, int port )
        { 
            pid=pid.ToUpper();

            if (slDeviceSdk.ContainsKey(ip))
                slDeviceSdk[ip] = pid;
            else
                slDeviceSdk.Add(ip, pid);

            if (pid == "VZSDK")
            {
                return VzSDK.SetListen(caption,ip, user, pass, port);
            }

            return -1;
        
        }

        public static int SetGateStatus(string ip, bool isOpen)
        {
            if (!slDeviceSdk.ContainsKey(ip))
                return -1;

            string pid = slDeviceSdk[ip];
            if (pid == "VZSDK")
            {
                return VzSDK.SetGateStatus(ip, isOpen);
            }

            return -1;
        }

        public static int GetHistory(string ip, string carID, string startTime, string endTime)
        {
            if (!slDeviceSdk.ContainsKey(ip))
                return -1;

            string pid = slDeviceSdk[ip];
            if (pid == "VZSDK")
            {
                return VzSDK.GetHistory(ip, carID, startTime, endTime);

            }

            return -1;
        }

        public static int SetOSD(string ip, string osd)
        {
            if (!slDeviceSdk.ContainsKey(ip))
                return -1;

            string pid = slDeviceSdk[ip];
            if (pid == "VZSDK")
            {
                return VzSDK.SetOSD(ip, osd);

            }

            return -1;
        }

        public static int SetTime(string ip, DateTime dt)
        {
            if (!slDeviceSdk.ContainsKey(ip))
                return -1;

            string pid = slDeviceSdk[ip];
            if (pid == "VZSDK")
            {
                return VzSDK.SetTime(ip, dt);

            }

            return -1;
        }

        public static int SetManualSnapshot(string ip)
        {
            if (!slDeviceSdk.ContainsKey(ip))
                return -1;
            string pid = slDeviceSdk[ip];

            if (pid == "VZSDK")
            {
                return VzSDK.SetManualSnapshot(ip);

            }

            return -1;
        }

        public static Image GetSnapShootToJpeg(string ip)
        {
            if (!slDeviceSdk.ContainsKey(ip))
                return null;

            string pid = slDeviceSdk[ip];

            if (pid == "VZSDK")
            {
                return VzSDK.GetSnapShootToJpeg(ip); 
            }

            return null;
        }

        public static int PlayPreview( string ip, IntPtr handle)
        {
            if (!slDeviceSdk.ContainsKey(ip))
                return -1;

            string pid = slDeviceSdk[ip];
            if (pid == "VZSDK")
            {
                return VzSDK.PlayPreview(ip, handle);
            }

            return -1;
        }

        public static int StopPreview(string ip,IntPtr handle)
        {
            if (!slDeviceSdk.ContainsKey(ip))
                return -1;
            string pid = slDeviceSdk[ip];

            if (pid == "VZSDK")
            {
                return VzSDK.StopPreview(ip, (int)handle);
            }

            return -1;
        }
        
        public static int SetFree(string ip)
        {
            if (!slDeviceSdk.ContainsKey(ip))
                return -1;
            string pid = slDeviceSdk[ip];

            if (pid == "VZSDK")
            {
                return VzSDK.SetFree(ip);
            }

            return -1;
        }

        public static int SetText(string ip, int line, DisplayMehod method, string text)
        {
            if (!slDeviceSdk.ContainsKey(ip))
                return -1;
            string pid = slDeviceSdk[ip];

            if (pid == "VZSDK")
            {
                return VzSDK.SetText(ip, line, method, text);
            }

            return -1;
        }

        public static int SetText2(string ip, int line, DisplayMehod method, string text)
        {
            if (!slDeviceSdk.ContainsKey(ip))
                return -1;
            string pid = slDeviceSdk[ip];

            if (pid == "VZSDK")
            {
                return VzSDK.SetText2(ip, line, method, text);
            }

            return -1;
        }

        public static int SetText(string ip, string text)
        {
            if (!slDeviceSdk.ContainsKey(ip))
                return -1;
            string pid = slDeviceSdk[ip];

            if (pid == "VZSDK")
            {
                return VzSDK.SetText(ip, text);
            }

            return -1;
        }

        public static int SetQR(string ip, string text)
        {
            if (!slDeviceSdk.ContainsKey(ip))
                return -1;
            string pid = slDeviceSdk[ip];

            if (pid == "VZSDK")
            {
                return VzSDK.SetQR(ip, text);
            }

            return -1;
        }

        public static int SetVolume(string ip, int leave)
        {
            if (!slDeviceSdk.ContainsKey(ip))
                return -1;
            string pid = slDeviceSdk[ip];

            if (pid == "VZSDK")
            {
                return VzSDK.SetVolume(ip, leave);
            }

            return -1;
        }

        public static int PlayVoice(string ip,string text)
        {
            if (!slDeviceSdk.ContainsKey(ip))
                return -1;
            string pid = slDeviceSdk[ip];

            if (pid == "VZSDK")
            {
                return VzSDK.PlayVoice(ip,text);
            }

            return -1;
        }

        public static int SetGreenLed( string ip, bool isRed)
        {
            if (!slDeviceSdk.ContainsKey(ip))
                return -1;
            string pid = slDeviceSdk[ip];

            if (pid == "VZSDK")
            {
                return VzSDK.SetGreenLed(ip, isRed);
            }

            return -1;
        }

        public static int SetLedTime(string ip,DateTime dt)
        {
            if (!slDeviceSdk.ContainsKey(ip))
                return -1;
            string pid = slDeviceSdk[ip];

            if (pid == "VZSDK")
            {
                return VzSDK.SetLedTime(ip, dt);
            }

            return -1;
        }

        public static int SetCarTour(string ip, bool isOpen)
        {
            if (!slDeviceSdk.ContainsKey(ip))
                return -1;
            string pid = slDeviceSdk[ip];

            if (pid == "VZSDK")
            {
                return VzSDK.SetCarTour(ip, isOpen);
            }

            return -1;
        }

        public static int SetTempCar(string ip, bool isAllow)
        {
            if (!slDeviceSdk.ContainsKey(ip))
                return -1;
            string pid = slDeviceSdk[ip];

            if (pid == "VZSDK")
            {
                return VzSDK.SetTempCar(ip, isAllow);
            }

            return -1;
        }

        public static int SetPoliceCar(string ip, bool isAllow)
        {
            if (!slDeviceSdk.ContainsKey(ip))
                return -1;
            string pid = slDeviceSdk[ip];

            if (pid == "VZSDK")
            {
                return VzSDK.SetPoliceCar(ip, isAllow);
            }

            return -1;
        }

        public static int SetGateType(string ip, RTSDK.DeviceDirection dir)
        {
            if (!slDeviceSdk.ContainsKey(ip))
                return -1;
            string pid = slDeviceSdk[ip];

            if (pid == "VZSDK")
            {
                return VzSDK.SetGateType(ip, dir);
            }

            return -1;
        }
    }
}
